package weibo4j.model;

import java.util.ArrayList;
import java.util.List;

import weibo4j.http.Response;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

public class ShortUrl {
	private  String url_short; //短链接
	private  String url_long; //原始长链接
	private int type;       //链接的类型，0：普通网页、1：视频、2：音乐、3：活动、5、投票   
	private boolean result; //短链的可用状态，true：可用、false：不可用。
	
	public ShortUrl(JSONObject json) throws JSONException{
		constructJson(json);
	}
	
	public void constructJson(JSONObject json) throws JSONException{
		url_short = json.getString("url_short");
		url_long = json.getString("url_long");
		type = json.getInt("type");
		result = json.getBoolean("result");
	}
	
	
	public static ShortUrlWapper ShortUrlWapper(Response res) throws WeiboException{
		JSONObject jsonShortUrl = res.asJSONObject(); 
		JSONArray shortUrlJson = null;
		try{
			if(!jsonShortUrl.isNull("urls")){				
				shortUrlJson = jsonShortUrl.getJSONArray("urls");
			}
			int size = shortUrlJson.length();
			List<ShortUrl> shortUrls = new ArrayList<ShortUrl>(size);
			for(int i = 0; i < size; i++){
				shortUrls.add(new ShortUrl(shortUrlJson.getJSONObject(i)));
			}
			return new ShortUrlWapper(shortUrls);
		}catch(JSONException jsone){
			throw new WeiboException(jsone);
		}
	}
	
	public String toString(){
		return "[url_short:"+url_short+";url_long:"+url_long+";type:"+type+";result:"+result;
	}
	public String getUrl_short() {
		return url_short;
	}
	public void setUrl_short(String url_short) {
		this.url_short = url_short;
	}
	public String getUrl_long() {
		return url_long;
	}
	public void setUrl_long(String url_long) {
		this.url_long = url_long;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public boolean isResult() {
		return result;
	}
	public void setResult(boolean result) {
		this.result = result;
	}
	
	
}
